package com.entmanage.base.hrm.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.entmanage.base.hrm.domain.HrmEmployee;
import com.entmanage.base.hrm.domain.HrmEmployeeCertificate;
import com.entmanage.base.hrm.domain.HrmEmployeeEducation;
import com.entmanage.base.hrm.domain.HrmEmployeeExperience;
import com.entmanage.base.hrm.mapper.HrmEmployeeMapper;
import com.entmanage.base.hrm.service.IHrmEmployeeService;
import com.entmanage.common.utils.DateUtils;
import com.entmanage.common.utils.SecurityUtils;
import com.entmanage.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import org.springframework.transaction.annotation.Transactional;


/**
 * 员工基本信息主Service业务层处理
 *
 * @author lsq
 * @date 2025-07-24
 */
@Service
public class HrmEmployeeServiceImpl implements IHrmEmployeeService {
    @Autowired
    private HrmEmployeeMapper hrmEmployeeMapper;

    /**
     * 查询员工基本信息主
     *
     * @param empId 员工基本信息主主键
     * @return 员工基本信息主
     */
    @Override
    public HrmEmployee selectHrmEmployeeByEmpId(Long empId) {
        return hrmEmployeeMapper.selectHrmEmployeeByEmpId(empId);
    }

    /**
     * 查询员工基本信息主列表
     *
     * @param hrmEmployee 员工基本信息主
     * @return 员工基本信息主
     */
    @Override
    public List<HrmEmployee> selectHrmEmployeeList(HrmEmployee hrmEmployee) {
        return hrmEmployeeMapper.selectHrmEmployeeList(hrmEmployee);
    }

    /**
     * 新增员工基本信息主
     *
     * @param hrmEmployee 员工基本信息主
     * @return 结果
     */
    @Transactional
    @Override
    public int insertHrmEmployee(HrmEmployee hrmEmployee) {
        // 保存用户id和部门id
        hrmEmployee.setUserId(SecurityUtils.getUserId());
        hrmEmployee.setDeptId(SecurityUtils.getDeptId());

        hrmEmployee.setCreateTime(DateUtils.getNowDate());
        int rows = hrmEmployeeMapper.insertHrmEmployee(hrmEmployee);
        insertHrmEmployeeEducation(hrmEmployee);
        insertHrmEmployeeExperience(hrmEmployee);
        insertHrmEmployeeCertificate(hrmEmployee);
        return rows;
    }

    /**
     * 修改员工基本信息主
     *
     * @param hrmEmployee 员工基本信息主
     * @return 结果
     */
    @Transactional
    @Override
    public int updateHrmEmployee(HrmEmployee hrmEmployee) {
        hrmEmployee.setUpdateTime(DateUtils.getNowDate());
        hrmEmployee.setUpdateBy(SecurityUtils.getUsername());
        hrmEmployeeMapper.deleteHrmEmployeeEducationByEmpId(hrmEmployee.getEmpId());
        insertHrmEmployeeEducation(hrmEmployee);
        hrmEmployeeMapper.deleteHrmEmployeeExperienceByEmpId(hrmEmployee.getEmpId());
        insertHrmEmployeeExperience(hrmEmployee);
        hrmEmployeeMapper.deleteHrmEmployeeCertificateByEmpId(hrmEmployee.getEmpId());
        insertHrmEmployeeCertificate(hrmEmployee);
        return hrmEmployeeMapper.updateHrmEmployee(hrmEmployee);
    }

    /**
     * 批量删除员工基本信息主
     *
     * @param empIds 需要删除的员工基本信息主主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteHrmEmployeeByEmpIds(Long[] empIds) {
        hrmEmployeeMapper.deleteHrmEmployeeExperienceByEmpIds(empIds);
        hrmEmployeeMapper.deleteHrmEmployeeEducationByEmpIds(empIds);
        hrmEmployeeMapper.deleteHrmEmployeeCertificateByEmpIds(empIds);
        return hrmEmployeeMapper.deleteHrmEmployeeByEmpIds(empIds);
    }

    /**
     * 删除员工基本信息主信息
     *
     * @param empId 员工基本信息主主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteHrmEmployeeByEmpId(Long empId) {
        hrmEmployeeMapper.deleteHrmEmployeeExperienceByEmpId(empId);
        hrmEmployeeMapper.deleteHrmEmployeeEducationByEmpId(empId);
        hrmEmployeeMapper.deleteHrmEmployeeCertificateByEmpId(empId);
        return hrmEmployeeMapper.deleteHrmEmployeeByEmpId(empId);
    }

    /**
     * 新增员工工作经历子信息
     *
     * @param hrmEmployee 员工基本信息主对象
     */
    public void insertHrmEmployeeExperience(HrmEmployee hrmEmployee) {
        List<HrmEmployeeExperience> hrmEmployeeExperienceList = hrmEmployee.getHrmEmployeeExperienceList();
        Long empId = hrmEmployee.getEmpId();
        if (StringUtils.isNotNull(hrmEmployeeExperienceList)) {
            List<HrmEmployeeExperience> list = new ArrayList<HrmEmployeeExperience>();
            for (HrmEmployeeExperience hrmEmployeeExperience : hrmEmployeeExperienceList) {
                hrmEmployeeExperience.setEmpId(empId);
                list.add(hrmEmployeeExperience);
            }
            if (list.size() > 0) {
                hrmEmployeeMapper.batchHrmEmployeeExperience(list);
            }
        }
    }

    /**
     * 新增员工教育背景子信息
     *
     * @param hrmEmployee 员工基本信息主对象
     */
    public void insertHrmEmployeeEducation(HrmEmployee hrmEmployee) {
        List<HrmEmployeeEducation> hrmEmployeeEducationList = hrmEmployee.getHrmEmployeeEducationList();
        Long empId = hrmEmployee.getEmpId();
        if (StringUtils.isNotNull(hrmEmployeeEducationList)) {
            List<HrmEmployeeEducation> list = new ArrayList<HrmEmployeeEducation>();
            for (HrmEmployeeEducation hrmEmployeeEducation : hrmEmployeeEducationList) {
                hrmEmployeeEducation.setEmpId(empId);
                list.add(hrmEmployeeEducation);
            }
            if (list.size() > 0) {
                hrmEmployeeMapper.batchHrmEmployeeEducation(list);
            }
        }
    }


    /**
     * 新增员工证书子信息
     *
     * @param hrmEmployee 员工基本信息主对象
     */
    public void insertHrmEmployeeCertificate(HrmEmployee hrmEmployee) {
        List<HrmEmployeeCertificate> hrmEmployeeCertificateList = hrmEmployee.getHrmEmployeeCertificateList();
        Long empId = hrmEmployee.getEmpId();
        if (StringUtils.isNotNull(hrmEmployeeCertificateList)) {
            List<HrmEmployeeCertificate> list = new ArrayList<HrmEmployeeCertificate>();
            for (HrmEmployeeCertificate hrmEmployeeCertificate : hrmEmployeeCertificateList) {
                hrmEmployeeCertificate.setEmpId(empId);
                list.add(hrmEmployeeCertificate);
            }
            if (list.size() > 0) {
                hrmEmployeeMapper.batchHrmEmployeeCertificate(list);
            }
        }
    }
    @Override
    public HrmEmployee selectEmployeeByUserId(Long userId) {
        return hrmEmployeeMapper.selectEmployeeByUserId(userId);
    }

    @Override
    public List<HrmEmployee> getEmployeesWithBirthdayOn(int month, int day) {
        return hrmEmployeeMapper.selectEmployeesWithBirthdayOn(month, day);
    }

    @Override
    public List<HrmEmployee> selectHrmEmployeeByIds(Long[] ids) {
        return hrmEmployeeMapper.selectHrmEmployeeByIds(ids);
    }

    @Override
    public List<HrmEmployee> listByStatusAndDelFlag(Integer status, Integer delFlag) {
        return hrmEmployeeMapper.selectList(status,delFlag);
    }

    @Override
    public List<HrmEmployee> listByDelFlag(Integer delFlag) {
        return hrmEmployeeMapper.findByDelFlag(delFlag);
    }

}
